package com.zbkj.service.util;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class SmsTemplateAly {
    private static final Logger logger = LoggerFactory.getLogger(SmsTemplateAly.class);

    // 阿里云核心配置（从配置文件注入）

    private static String accessKeyId="LTAI5tKeQREbkUERUfzLYRYn";


    private static String accessKeySecret="QFOTYDZwnSN0NJvszleYFYikr7Zo02";

// 与签名/模板地域一致（如 cn-shanghai）
    private static String regionId="cn-shenzhen";

// 已审核通过的短信签名
    private static String signName = "星娱世界深圳文化科技";

// 已审核通过的验证码模板（含${code}占位符）
    private static String templateCode = "SMS_498605423";

/*    public static void main(String[] strings){
       // System.out.println(getCode("555555","13640660742"));
        getCode("555555","13640660742");
    }*/
    public static Boolean getCode(String codeNo, String phone) {
        logger.info("准备发送验证码：{}，目标手机号：{}", codeNo, phone);

        // 1. 初始化阿里云SMS客户端
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        // 2. 构建发送请求（直接使用入参，无任何额外校验）
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phone); // 已校验合法的手机号
        request.setSignName(signName); // 已审核通过的签名
        request.setTemplateCode(templateCode); // 已审核通过的模板
        request.setTemplateParam("{\"code\":\"" + codeNo + "\"}"); // 传入预设验证码
        SendSmsResponse response = null;

        try {
            // 3. 调用阿里云API发送短信
            response = acsClient.getAcsResponse(request);
        } catch (ClientException e) {
            throw new RuntimeException(e);
        }

        // 4. 校验发送结果
        if ("OK".equals(response.getCode())) {
            logger.info("验证码发送成功！手机号：{}，验证码：{}，请求ID：{}",
                    phone, codeNo, response.getRequestId());
            return true;
        } else {
            logger.error("验证码发送失败！手机号：{}，验证码：{}，错误码：{}，错误信息：{}，请求ID：{}",
                    phone, codeNo, response.getCode(), response.getMessage(), response.getRequestId());
            throw new RuntimeException("短信发送失败：" + response.getMessage());
        }
    }
}
